import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const route = new Router({
  routes: [
    {
      path: '/index',
      name: 'Index',
      component: () => import('pages/Index/Index'),
      meta: {
        showLeftMenu: true,
        showHeader: true,
        title: '首页'
      }
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('pages/Login/Login'),
      meta: {
        showLeftMenu: false,
        showHeader: false,
        title: '登录'
      }
    },
    {
      path: '/',
      redirect: to => {
        let urls = localStorage.getItem('pageUrls')
        if (urls) {
          let urlArr = urls.split(',')
          return urlArr[0]
        } else {
          return '/login'
        }
      }
    }
  ]
})

route.beforeEach((to, from, next) => {
  let title = to.meta.title
  if (title) {
    document.title = title
  }
  next()
})

const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default route
